package cn.wolfcode.car.utils;

import com.alibaba.fastjson.JSON;
import lombok.Cleanup;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class JsonUtil {
    public static String objToJson(Object object, Class clazz) {
        HashMap<String, Object> map = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            try {
                Object o = field.get(object);
                map.put(name, o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return JSON.toJSONString(map);
    }
}
